package com.zzyl.serve.controller.member;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.utils.UserThreadLocal;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.serve.domain.Reservation;
import com.zzyl.serve.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 * @date 2025-05-08
 */
@Slf4j
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    @ApiOperation("查询当天取消预约数量")
    @GetMapping("/cancelled-count")
    public AjaxResult getCancelledCount() {
        long time = System.currentTimeMillis();
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long userId = UserThreadLocal.getUserId();
        Integer num = reservationService.getCancelledCount(startTime, endTime, userId);
        return AjaxResult.success(num);
    }

    @ApiOperation("2.2 查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public AjaxResult getCountByTime(Long time) {
        //根据结构来看，这个方法是我点击预约的时候，就会判断我约了在这个时间段的有几个人了，每超过一个人就是6-N次
        List<Map<String, Object>> list =  reservationService.getCountByTime(time);
        return AjaxResult.success("操作成功", list);
    }

    @ApiOperation("2.3 新增预约")
    @PostMapping()
    public AjaxResult insertReservation(@RequestBody Reservation reservation) {
        reservationService.insertReservation(reservation);
        return AjaxResult.success();
    }
    @ApiOperation("2.4 分页查询预约")
    @GetMapping("/page")
    public AjaxResult getResvationPageList(
                                           @RequestParam(required = false) Integer status) {
        startPage();
        List<Reservation> list = reservationService.list(new LambdaQueryWrapper<Reservation>()
                .eq(ObjectUtil.isNotEmpty(status), Reservation::getStatus, status));
        return AjaxResult.success(getDataTable(list));
    }
    @ApiOperation("2.5 取消预约")
    @PutMapping("/{id}/cancel")
    public AjaxResult updateResvationStatus(@PathVariable Long id) {
        reservationService.update(new LambdaUpdateWrapper<Reservation>()
                .eq(Reservation::getId, id)
                .set(Reservation::getStatus, 2));
        return AjaxResult.success();
    }
}
